<?php

namespace app\common\model\User;

use think\Model;

/**
 * 用户收货地址
 */
class Address extends Model{
    protected $table="pre_user_address";
    
    protected $autoWriteTimestamp=false;

    protected $createTime=false;

    protected $updateTime=false;

    public function getRegionAttr($value,$data){
        $province=model('Region')->where(['code'=>$data['province']])->find();
        $city=model('Region')->where(['code'=>$data['city']])->find();
        $district=model('Region')->where(['code'=>$data['district']])->find();

        $output=[];
        if($province){
            $output[]=$province['name'];
        }
        if($city){
            $output[]=$city['name'];
        }
        if($district){
            $output[]=$district['name'];
        }

        return implode('-',$output);
    }

    // 给模型定义一个管理查询
    public function provinces(){
        return $this->belongsTo('app\common\model\Region','province','code',[],'LEFT')->setEagerlyType(1);
    }

    public function citys(){
        return $this->belongsTo('app\common\model\Region','city','code',[],'LEFT')->setEagerlyType(1);
    }

    public function districts(){
        return $this->belongsTo('app\common\model\Region','district','code',[],'LEFT')->setEagerlyType(1);
    }
}